package com.mmall.controller.portal;

import com.mmall.common.IConst;
import com.mmall.common.ResponseInfo;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 用户控制层
 * Created by Administrator on 2017/9/21-23:39-23:39.
 */
@Controller
@RequestMapping("/user/")
public class UserController {

    @Resource
    private IUserService iUserService;

    /**
     * 用户登录
     * @param username 　用户名
     * @param password 　用户密码
     * @param session  　回话
     * @return todo
     */
    @ResponseBody
    @RequestMapping(value = "login.do", method = RequestMethod.POST)
    public ServerResponse<User> login(String username, String password, HttpSession session, String a) {
        ServerResponse<User> response = iUserService.login(username, password);
        if (response.isSuccess()) session.setAttribute(IConst.CURRENT_USER, response.getData());
        return response;
    }

    /**
     * 用户退出
     * @param session 　回话
     * @return 返回成功代码
     */
    @ResponseBody
    @RequestMapping(value = "quit.do", method = RequestMethod.POST)
    public ServerResponse<String> quit(HttpSession session) {
        session.removeAttribute(IConst.CURRENT_USER);
        return ServerResponse.createBySuccess();
    }

    /**
     * 用户姓名,邮箱,电话其中一个是否重复验证
     * 可以用于在注册过程中,同步验证前一个信息是否存在.
     * @param field 字段信息
     * @param type  字段名称
     * @return 响应
     */
    @ResponseBody
    @RequestMapping(value = "check_valid.do", method = RequestMethod.POST)
    public ServerResponse checkValid(String field, String type) {
        return iUserService.checkValid(field, type);
    }

    /**
     * 注册提交后三个字段全部验证
     * @param user 自动包装的用户信息
     * @return 响应对象
     */
    @ResponseBody
    @RequestMapping(value = "register.do", method = RequestMethod.POST)
    public ServerResponse register(User user) {
        System.out.println("---------");
        return iUserService.register(user);
    }

    /**
     * 获取用户信息
     *
     * @param session 会话
     * @return 响应对象
     */
    @ResponseBody
    @RequestMapping(value = "get_user_info.do", method = RequestMethod.POST)
    public ServerResponse<User> getUserInfo(HttpSession session) {
        User user = (User) session.getAttribute(IConst.CURRENT_USER);
        if (user != null) return ServerResponse.createBySuccess(user);
        return ServerResponse.createByErrorMessage("用户未登录,无法获取当前用户信息");
    }

    /**
     * 获取忘记密码问题
     *
     * @param username 用户名
     * @return 响应对象
     */
    @ResponseBody
    @RequestMapping(value = "forget_get_question.do", method = RequestMethod.POST)
    public ServerResponse<String> forgetGetQuestion(String username) {
        return iUserService.selectQuestion(username);
    }

    /**
     * 验证忘记密码问题的答案
     *
     * @param username 用户名
     * @param question 用户忘记密码问题
     * @param answer   忘记密码问题答案
     * @return 响应对象
     */
    @ResponseBody
    @RequestMapping(value = "forget_check_answer.do", method = RequestMethod.POST)
    public ServerResponse<String> forgetCheckAnswer(String username, String question, String answer) {
        return iUserService.checkAnswer(username, question, answer);
    }

    /**
     * 验证忘记密码问题后重置密码
     *
     * @param username    用户名
     * @param passwordNew 重新设置的密码
     * @param forgetToken 验证答案后获取的UUID字符串
     * @return 响应对象
     */
    @ResponseBody
    @RequestMapping(value = "forget_reset_password.do", method = RequestMethod.POST)
    public ServerResponse<String> forgetResetPassword(String username, String passwordNew, String forgetToken) {
        return iUserService.forgetResetPassword(username, passwordNew, forgetToken);
    }

    /**
     * 登录状态重置密码
     *
     * @param session     当前回话
     * @param passwordOld 旧密码
     * @param passwordNew 新密码
     * @return 响应对象
     */
    @ResponseBody
    @RequestMapping(value = "reset_password.do", method = RequestMethod.POST)
    public ServerResponse<String> resetPassword(HttpSession session, String passwordOld, String passwordNew) {
        User user = (User) session.getAttribute(IConst.CURRENT_USER);
        if (user == null) return ServerResponse.createByErrorMessage("用户未登录!");
        return iUserService.resetPassword(passwordOld, passwordNew, user);
    }

    /**
     * 更新用户信息
     *
     * @param session 当前回话
     * @param newUser 新用户信息封装对象
     * @return 响应对象
     */
    @ResponseBody
    @RequestMapping(value = "update_info.do", method = RequestMethod.POST)
    public ServerResponse<User> updateInfo(HttpSession session, User newUser) {
        User user = (User) session.getAttribute(IConst.CURRENT_USER);
        if (user == null) return ServerResponse.createByErrorMessage("用户未登录!");
        //新的用户信息中没有sql自动生成的id信息.需要手动添加
        newUser.setId(user.getId());
        //将用户名添加到新用户信息中
        newUser.setUsername(user.getUsername());
        ServerResponse<User> response = iUserService.updateInfo(newUser);
        if (response.isSuccess()) session.setAttribute(IConst.CURRENT_USER, response.getData());
        return response;
    }

    /**
     * 获取当前用户信息
     *
     * @param session 当前会话
     * @return 响应对象
     */
    @ResponseBody
    @RequestMapping(value = "get_info.do", method = RequestMethod.POST)
    public ServerResponse<User> getInfo(HttpSession session) {
        User user = (User) session.getAttribute(IConst.CURRENT_USER);
        if (user == null)
            return ServerResponse.createByErrorCode(ResponseInfo.NEED_LOGIN.getCode(), "status=10,前台控制跳转到登录页面!");
        return iUserService.getInfo(user.getId());
    }
}